<?php
require_once ('..'.DIRECTORY_SEPARATOR.'initialize.php');
include_once(Utils::getPath(array('shared', 'template.php')));
include_once(Utils::getPath(array('repositories', 'userRepository.php')));
include_once(Utils::getPath(array('repositories', 'messageRepository.php')));

$template = new Template('application');
$template->initialize();

$messageRep = new MessageRepository();

$page = !isset($_GET['page']) ? 0 : ($_GET['page'] == 0 ? 0 : $_GET['page'] - 1);
$filterUrlParam = '';

if (isset($_GET['filter'])) {
    if ($_GET['filter'] == 'in') {
        $messages = $messageRep->findByReceiver($_SESSION['authenticatedId'], null, 'Date', true, $page, 25);
        $filterUrlParam = '&amp;filter=in';
        $title = "Inbox";
    } else if ($_GET['filter'] == 'out') {
        $messages = $messageRep->findBySender($_SESSION['authenticatedId'], null, 'Date', true, $page, 25);
        $filterUrlParam = '&amp;filter=out';
        $title = "Outbox";
    }
    else{
        Utils::goToErrorPage(StatusCodes::HTTP_BAD_REQUEST);
    }
} else {
    $messages = $messageRep->findByUsername($_SESSION['authenticatedId'], null, 'Date', true, $page, 25);
    $title = "All messages";
}

$count = $messageRep->countLast();
$pageMax = ceil($count / 25);

$userRep = new UserRepository();
$messageList = array();
if ($messages != null) {
    foreach ($messages as $value) {
        $messageElement = array();
        $messageElement['toMe'] = $value->getIdReceiver() == $_SESSION['authenticatedId'];
        $messageElement['from'] = $userRep->getById($value->getIdSender())->getUserName();
        $messageElement['to'] = $userRep->getById($value->getIdReceiver())->getUserName();
        $messageElement['urlfriend'] = Utils::getUrl("user/profile.php?id=");
        $messageElement['urlfriend'] .= $messageElement['toMe'] ? $value->getIdSender() : $value->getIdReceiver();
        $messageElement['urlview'] = 'view.php?id=' . $value->getId();
        $messageElement['urldelete'] = 'delete.php?id=' . $value->getId();
        $messageElement['datetime'] = Utils::getFormatTime($value->getDate());
        $messageElement['subject'] = $value->getSubject();
        $messageElement['message'] = Utils::splitString($value->getText(), 80);
        $messageElement['isRead'] = $value->getIsRead() || !$messageElement['toMe'];

        $messageList[] = $messageElement;
    }
} else {
    $emptyMessage = "No messages";
}
?>
<h2><?php echo $title; ?></h2>
<ul id="message-filter-mode">
    <li><a href="list.php">All</a></li>
    <li><a href="list.php?filter=in">Inbox</a></li>
    <li><a href="list.php?filter=out">Outbox</a></li>
</ul>
<?php
if (count($messageList) == 0)
    echo $emptyMessage;
else {
    ?>
        <table id="list">
            <tr>
                <th>Username</th>
                <th>Subject</th>
                <th>Date</th>
                <th>Delete</th>
            </tr>
        <?php
        foreach ($messageList as $message) {
            ?>
            <tr class="<?php echo $message['isRead'] ? "list-item message-read" : "list-item message-not-read" ?>" onclick="location.href='<?php echo $message['urlview']; ?>'">
                <?php if ($message['toMe']) { ?> 
                    <td class ="message-from">
                        <a href="<?php echo $message['urlfriend']; ?>">
                            <?php echo $message['from'] ?></a></td>
                <?php } else { ?>
                    <td class ="message-to">To: <a href="<?php echo $message['urlfriend']; ?>">
                            <?php echo $message['to'] ?></a></td>
                <?php } ?>
                <td class ="message-subject">
                    <a href="<?php echo $message['urlview'] ?>" ><?php echo $message['subject'] ?></a> - <?php echo $message['message'] ?>
                </td>
                <td class ="message-datetime"><?php echo $message['datetime'] ?></td>
                <td class ="message-delete">
                    <?php if(!$message['toMe']){ ?><a href="<?php echo $message['urldelete'] ?>" >Delete</a>
                    <?php }else{ ?>
                        Not allowed
                    <?php } ?>
                </td>
            </tr>
        <?php } ?>

    </table>
    <?php
}

Utils::pagination($page, $pageMax, Utils::getUrl('message/list.php'), $filterUrlParam);
$template->createPage($title);
?>